﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace NhJump.Persistence.Batching
{
    /// <summary>
    /// Uses NHibernate's MultiCriteria feature to perform the outstanding batch operations in
    /// one database round-trip
    /// </summary>
    public class MultiCriteriaBatchExecuter : IBatchExecuter
    {
        private readonly ISession _session;

        public MultiCriteriaBatchExecuter(ISession session)
        {
            _session = session;
        }

        public IList Execute(IList<CriteriaWithResultAction> batch)
        {
            var multi = _session.CreateMultiCriteria();

            foreach (var criteriaAndAction in batch)
            {
                multi.Add(criteriaAndAction.Criteria);
            }

            return multi.List();
        }
    }
}
